package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import com.qf.pojo.BaseResp;
import com.qf.pojo.Play;
import com.qf.service.PlayService;
import com.qf.utils.UploadToken;
import com.qf.utils.UploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.util.Map;

@RestController
@RequestMapping("/play")
public class PlayController {

    @Autowired
    PlayService playService;

    @RequiresPermissions(value = {"play:select"})
    @RequestMapping(value = "/findall", method = RequestMethod.POST)
    public BaseResp findall(@RequestBody Map map) {
        return playService.playall(Integer.valueOf(map.get("page").toString()), Integer.valueOf(map.get("size").toString()));
    }

    @PostMapping("/upload")
    @ResponseBody
    public BaseResp upload(@RequestParam("file") MultipartFile multipartFile) {
        String upload = UploadUtils.upload(multipartFile);
        // System.out.println(map);
        BaseResp baseResp = new BaseResp();
        baseResp.setData(upload);
        return baseResp;
    }

    @GetMapping("/gettoken")
    public BaseResp gettoken() {
        String token = UploadToken.upload();
        BaseResp baseResp = new BaseResp();
        baseResp.setData(token);
        return baseResp;
    }

    @RequiresPermissions(value = {"play:insert"})
    @PostMapping("/add")
    public BaseResp add(@RequestBody Play play) {
        return playService.playadd(play);
    }

    @RequiresPermissions(value = {"play:delete"})
    @PostMapping("delete")
    public BaseResp delete(@RequestBody String str) {

        int id = Integer.parseInt(JSON.parseObject(str).get("id").toString());
        System.out.println(id);
        return playService.delete(id);

    }
}
